//
//  ACUserProfileManager.m
//  ACLiteAVUISDK-ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/4/12.
//

#import "ACUserProfileManager.h"

@implementation ACUserProfileManager

+ (instancetype)shareInstance{
    static ACUserProfileManager *_shareInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shareInstance = [[super alloc] init];
    });
    return _shareInstance;
}

- (void)setUserModel:(ACUserModel *)userModel{
    if (userModel.token != nil) {
        _userModel = userModel;
    }else{
        NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"userData"];
        _userModel = [ACUserModel mj_objectWithKeyValues:dic];
    }
    [[NSUserDefaults standardUserDefaults] setObject:[_userModel mj_JSONString] forKey:@"userData"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(ACUserModel *)getUserModel{
    @synchronized (self) {
        NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"userData"];
        _userModel = [ACUserModel mj_objectWithKeyValues:dic];
        if (_userModel == nil) {
            _userModel = [ACUserModel new];
        }
        return _userModel;
    }
}

-(BOOL)getLoginStatus{
    @synchronized (self) {
        ACUserModel *userModel = [self getUserModel];
        if (![userModel.user_id isEqualToString:@""] && userModel.user_id != nil) {
            BOOL result = [TRTCLiveRoomIMAction setupSDKWithSDKAppID:ACSDKAPPID listener:self];
            if (!result) {
                return NO;
            }
            return YES;
        }
        return NO;
    }
}

-(NSString *)getToken{
    ACUserModel *userModel = [self getUserModel];
    if (userModel == nil) {
        return @"";
    }
    return  userModel.token;;
}

-(void)setUserSig:(NSString *)userSig{
    if (userSig != nil && ![userSig isEqualToString:@""]) {
        _userSig = userSig;
    }else{
        NSString *dicInfo = [[NSUserDefaults standardUserDefaults] objectForKey:@"userSig"];
        _userSig = dicInfo;
    }
    [[NSUserDefaults standardUserDefaults] setObject:_userSig forKey:@"userSig"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(NSString *)getUserSig{
    @synchronized (self) {
        NSString *dicInfo = [[NSUserDefaults standardUserDefaults] objectForKey:@"userSig"];
        _userSig = dicInfo;
        if (_userSig == nil) {
            _userSig = @"";
        }
        return _userSig;
    }
}

-(void)removeUserCache{
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"userData"];
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"userSig"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

#pragma mark -- IM 登录状态回调
-(void)onConnectSuccess{
    
}
-(void)onConnecting{
    
}
-(void)onConnectFailed:(int)code err:(NSString *)err{
    
}
-(void)onKickedOffline{
    DDLogInfo(@"onKickedOffline----您被挤下线了");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"onKickedOffline" object:nil];
}
-(void)onUserSigExpired{
    
}

@end



@interface ACUserModel ()

@end
@implementation ACUserModel

- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:self.nickname forKey:@"nickname"];
    [coder encodeObject:self.avatar forKey:@"avatar"];
    [coder encodeObject:self.token forKey:@"token"];
    [coder encodeObject:self.user_id forKey:@"user_id"];
    [coder encodeObject:self.name forKey:@"name"];
    [coder encodeObject:self.expireTime forKey:@"expireTime"];
    [coder encodeObject:self.phone forKey:@"phone"];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super init];
    if (self) {
        self.nickname = [coder decodeObjectForKey:@"nickname"];
        self.avatar = [coder decodeObjectForKey:@"avatar"];
        self.token = [coder decodeObjectForKey:@"token"];
        self.user_id = [coder decodeObjectForKey:@"user_id"];
        self.name = [coder decodeObjectForKey:@"name"];
        self.expireTime = [coder decodeObjectForKey:@"expireTime"];
        self.phone = [coder decodeObjectForKey:@"phone"];
    }
    return self;
}

-(instancetype)init{
    if (self = [super init]) {
        
    }
    return self;
}
@end
